Imports System.io
Imports System.Text

Friend Class TextHandler

    Private EH As New ErrorHandler

    Friend Function TrimStringLength(ByVal strInput As String, Optional ByVal intMaxLineLen As Integer = 29) As String

        Dim intCurLen As Integer = strInput.Length
        If intCurLen >= intMaxLineLen Then
            strInput = strInput.Remove(intMaxLineLen - 3)
            strInput += "..."
        End If

        TrimStringLength = strInput

    End Function

    Friend Function WriteLineToFile(ByVal Line As String, ByVal File As String, ByVal Close As Boolean) As Boolean
        Using SW As StreamWriter = New StreamWriter(File)
            SW.WriteLine(Line)
            If Close = True Then
                SW.Close()
            End If
        End Using
        Return True
    End Function

    Friend Function AssembleTabDelString(ByVal BatchNum As String, ByVal ItemCode As String, ByVal WH As String) As String

        Dim RetStr As String = BatchNum & ControlChars.Tab & ItemCode & ControlChars.Tab & WH
        Return RetStr

    End Function

    Friend Function ReadFileLinesToArrayList(ByVal File As String) As ArrayList

        Dim AL As New ArrayList
        Dim Row As String()
        Dim Field As String

        Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(File)

            MyReader.TextFieldType = FileIO.FieldType.Delimited
            MyReader.SetDelimiters(",")

            While Not MyReader.EndOfData

                Row = MyReader.ReadFields()
                For Each Field In Row
                    AL.Add(Field)
                Next

            End While

        End Using

        Return AL

    End Function

    Friend Function BuildUpdateSQL(ByVal SplitVals As ArrayList, ByVal KeyVal As Object) As String

        Dim SB As New StringBuilder

        SB.Append("DECLARE @InDate As DateTime, ")
        SB.Append("@LotNo As nvarchar(10), ")
        SB.Append("@Repack As nvarchar(10), ")
        SB.Append("@BatchNum As nvarchar(32), ")
        SB.Append("@ItemCode As nvarchar(20), ")
        SB.Append("@ItemName As nvarchar(100), ")
        SB.Append("@Pieces As int, ")
        SB.Append("@TotalQty As numeric(19, 6), ")
        SB.Append("@Grade As nvarchar(10), ")
        SB.Append("@WhsCode As nvarchar(8) ")
        SB.Append("SET @ItemCode = '" & SplitVals.Item(0) & "' ")
        SB.Append("SET @BatchNum = '" & SplitVals.Item(1) & "' ")
        SB.Append("SET @WhsCode = '" & SplitVals.Item(2) & "' ")
        SB.Append("SELECT @InDate = InDate FROM OIBT WHERE (BatchNum = @BatchNum And ItemCode = @ItemCode And WhsCode = @WhsCode AND ItemCode NOT LIKE 'G%') ")
        SB.Append("SELECT @Repack = U_RepackOrder FROM OIBT WHERE (BatchNum = @BatchNum And ItemCode = @ItemCode And WhsCode = @WhsCode AND ItemCode NOT LIKE 'G%') ")
        SB.Append("SELECT @LotNo = U_LotN0 FROM OIBT WHERE (BatchNum = @BatchNum And ItemCode = @ItemCode And WhsCode = @WhsCode AND ItemCode NOT LIKE 'G%') ")
        SB.Append("SELECT @ItemName = ItemName FROM OIBT WHERE (BatchNum = @BatchNum And ItemCode = @ItemCode And WhsCode = @WhsCode AND ItemCode NOT LIKE 'G%') ")
        SB.Append("SELECT @Pieces = U_Pieces FROM OIBT WHERE (BatchNum = @BatchNum And ItemCode = @ItemCode And WhsCode = @WhsCode AND ItemCode NOT LIKE 'G%') ")
        SB.Append("SELECT @TotalQty = U_RecQty FROM OIBT WHERE (BatchNum = @BatchNum And ItemCode = @ItemCode And WhsCode = @WhsCode AND ItemCode NOT LIKE 'G%') ")
        SB.Append("SELECT @Grade = U_Grade FROM OIBT WHERE (BatchNum = @BatchNum And ItemCode = @ItemCode And WhsCode = @WhsCode AND ItemCode NOT LIKE 'G%') ")
        SB.Append("UPDATE EQP_BATCHLOG SET ItemCode = @ItemCode, ItemName = @ItemName, BatchNum = @BatchNum, WhsCode = @WhsCode, U_Pieces = @Pieces, U_TotalQty = @TotalQty, U_Grade = @Grade, InDate = @InDate, U_LotN0 = @LotNo, U_RepackOrder = @Repack WHERE TabDelKeyValues = '" & KeyVal.ToString & "'")

        Return SB.ToString

    End Function

    Friend Function BuildSelectSQL(ByVal BatchFrom As String, ByVal BatchTo As String, ByVal ItemFrom As String, ByVal ItemTo As String, ByVal LotFrom As String, ByVal LotTo As String, ByVal PrintStatus As String) As String

        Dim SB As New StringBuilder

        SB.Append("SELECT CONVERT(CHAR(11),InDate,101) As 'In Date', ")
        SB.Append("BatchNum As 'BatchNum', ")
        SB.Append("U_LotN0 As 'Lot No', ")
        SB.Append("ItemCode As 'Item Code', ")
        SB.Append("ItemName As 'Item Name', ")
        SB.Append("U_Pieces As 'Pieces', ")
        SB.Append("Cast(ROUND(U_TotalQty, 1) as numeric(19, 1)) As 'Yards', ")
        SB.Append("U_Grade As 'Grade', ")
        SB.Append("U_RepackOrder As 'Repack Order' ")
        SB.Append("FROM EQP_BatchLog ")
        SB.Append("WHERE (Batchnum BETWEEN '" & BatchFrom & "' AND '" & BatchTo & "' AND ")
        SB.Append("ItemCode BETWEEN '" & ItemFrom & "' AND '" & ItemTo & "' AND ")
        SB.Append("U_LotN0 BETWEEN '" & LotFrom & "' And '" & LotTo & "' AND ")
        SB.Append("WhsCode = '01' AND PrintStatus " & PrintStatus & ")")

        Return SB.ToString

    End Function

    Friend Function BreakApartTabDelStr(ByVal Input As String) As ArrayList

        Dim AL As New ArrayList
        Dim Len As Integer, i As Integer, TabCount As Integer
        Dim C As Char
        Dim SB1 As New StringBuilder

        If Input.Contains(ControlChars.Tab) Then
            Len = Input.Length

            For i = 0 To (Len - 1)
                C = Input.Chars(i)
                If Not C = (ControlChars.Tab) Then
                    SB1.Append(C)
                Else
                    TabCount += 1
                    Select Case TabCount
                        Case 1
                            AL.Add(SB1.ToString)
                            SB1 = New StringBuilder
                        Case 2
                            AL.Add(SB1.ToString)
                            SB1 = New StringBuilder
                    End Select
                End If
            Next

            AL.Add(SB1.ToString)
            Return AL

        Else
            Throw New Exception("String is not tab delimited.")
        End If

    End Function


    'Converts an object to a double if possible and then rounds the value to the nearest 1 decimal place
    Friend Function RoundToOneDec(ByVal Val As Object) As Double

        Dim Dub As Double

        Try
            Dub = CDbl(Val)
            Dub = Math.Round(Dub, 1)
            Return Dub
        Catch ex As Exception
            EH.RecordError(ex, False)
        End Try

    End Function

End Class
